import apiClient from "../apiClient";

export interface CategoryItem {
	id: number;
	name: string;
	weigh: number;
	status: 0 | 1;
	createtime: string;
	updatetime: string;
}

export interface ApiResponse<T> {
	total: number;
	items: T[];
}

export interface TaskItem {
	id: number;
	title: string;
	category_id: number;
	price: string | number;
	reviewer: string;
	status: number;
	taskFields: FormField[];
	createtime?: number;
	updatetime?: number;
	deletetime?: number | null;
	category?: {
		id: number;
		name: string;
		status: number;
	};
	status_text?: string;
}

export interface TaskAddData {
	title: string;
	category_id: number;
	price: string | number;
	reviewer: string;
	status: number;
	taskFields: FormField[];
}

export interface TaskEditData extends Partial<TaskAddData> {
	id: number;
}

export interface TaskSubmitRecord {
	id: number;
	task_id: number;
	user_id: number;
	content: string;
	status: number; // 0: 待审核, 1: 已通过, 2: 已驳回
	remark?: string;
	createtime?: number;
	updatetime?: number;
	task?: TaskItem;
	user?: {
		id: number;
		username: string;
	};
	prev?: {
		id: number;
		task_id: number;
		user_id: number;
		createtime: number;
		updatetime: number;
		status: number;
		reviewtime: number;
		reviewer: number;
		remark: string;
		description: string;
		task?: TaskItem;
	};
	next?: {
		id: number;
		task_id: number;
		user_id: number;
		createtime: number;
		updatetime: number;
		status: number;
		reviewtime: number;
		reviewer: number;
		remark: string;
		description: string | null;
		task?: TaskItem;
	};
}

export interface TaskSubmitData {
	task_id: number;
	content: string;
}

export enum TaskApi {
	CategoryList = "/task/task_category/index",
	CategoryAdd = "/task/task_category/add",
	CategoryEdit = "/task/task_category/edit",
	CategoryDelete = "/task/task_category/del",
	CategoryInfo = "/task/task_category/info",
	TaskAdd = "/task/task/add",
	TaskEdit = "/task/task/edit",
	TaskList = "/task/task/index",
	TaskInfo = "/task/task/info",
	TaskDelete = "/task/task/del",
	TaskChangeStatus = "/task/task/changeStatus",
	TaskSubmitRecord = "/task/task/records",
	TaskSubmit = "/task/submit/add",
	TaskSubmitReview = "/task/task/review",
	TaskSubmitInfo = "/task/task/recordInfo",
	TaskRecordExport = "/task/task/export",
}

// 任务分类相关接口
const categoryList = (params: { page: number; limit: number }) =>
	apiClient.get<ApiResponse<CategoryItem>>({
		url: TaskApi.CategoryList,
		params,
	});

const categoryAdd = (
	data: Omit<CategoryItem, "id" | "createtime" | "updatetime">,
) =>
	apiClient.post({
		url: TaskApi.CategoryAdd,
		data,
	});

const categoryEdit = (data: Partial<CategoryItem> & { id: number }) =>
	apiClient.post({
		url: TaskApi.CategoryEdit,
		data,
	});

const categoryDelete = (id: number) =>
	apiClient.post({
		url: TaskApi.CategoryDelete,
		data: { id },
	});

const categoryInfo = (id: number) =>
	apiClient.get<CategoryItem>({
		url: TaskApi.CategoryInfo,
		params: { id },
	});

const taskAdd = (data: TaskAddData) =>
	apiClient.post({
		url: TaskApi.TaskAdd,
		data,
	});

const taskEdit = (data: TaskEditData) =>
	apiClient.post({
		url: TaskApi.TaskEdit,
		data,
	});

const taskInfo = (id: number) =>
	apiClient.get<TaskItem>({
		url: TaskApi.TaskInfo,
		params: { id },
	});

const taskList = (params: {
	page: number;
	limit: number;
	title?: string;
	category_id?: number;
	status?: number;
}) =>
	apiClient.post<ApiResponse<TaskItem>>({
		url: `${TaskApi.TaskList}?page=${params.page}&limit=${params.limit}`,
		data: {
			title: params.title,
			category_id: params.category_id,
			status: params.status,
		},
	});

const taskDelete = (id: number) =>
	apiClient.post({
		url: TaskApi.TaskDelete,
		data: { id },
	});

const taskChangeStatus = (id: number, status: number) =>
	apiClient.post({
		url: TaskApi.TaskChangeStatus,
		data: { id, status },
	});

const taskSubmitList = (params: {
	page: number;
	limit: number;
	task_id?: number;
	start_time?: number;
	end_time?: number;
	review_start_time?: number;
	review_end_time?: number;
}) =>
	apiClient.get<ApiResponse<TaskSubmitRecord>>({
		url: TaskApi.TaskSubmitRecord,
		params,
	});

const taskSubmit = (data: TaskSubmitData) =>
	apiClient.post({
		url: TaskApi.TaskSubmit,
		data,
	});

const taskSubmitReview = (
	id: number,
	status: number,
	remark?: string,
	price?: number,
) =>
	apiClient.post({
		url: TaskApi.TaskSubmitReview,
		data: { id, status, remark, price },
	});

const taskSubmitInfo = (id: number) =>
	apiClient.get<TaskSubmitRecord>({
		url: TaskApi.TaskSubmitInfo,
		params: { id },
	});

const taskRecordExport = (params: any) =>
	apiClient.get({
		url: TaskApi.TaskRecordExport,
		params,
	});

export default {
	categoryList,
	categoryAdd,
	categoryEdit,
	categoryDelete,
	categoryInfo,
	taskAdd,
	taskEdit,
	taskInfo,
	taskList,
	taskDelete,
	taskChangeStatus,
	taskSubmitList,
	taskSubmit,
	taskSubmitReview,
	taskSubmitInfo,
	taskRecordExport,
};
